package com.caigou.modules.service.repository

import com.caigou.comm.base.bean.HttpResult
import com.caigou.modules.service.baen.PlatformItemBean
import com.caigou.modules.service.baen.WebsiteCollectBean
import com.caigou.modules.service.network.HttpServiceService
import dagger.hilt.android.scopes.ActivityRetainedScoped
import kotlinx.coroutines.flow.Flow
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/11/11
 */
@ActivityRetainedScoped
class ServiceRepository @Inject constructor() {

    fun getWebsiteCollectList(page: Long, limit: Long, userId: String): Flow<HttpResult<WebsiteCollectBean>> {
        return HttpServiceService.service.getWebsiteCollectList(page, limit, userId)
    }

    fun deleteWebsiteCollect(userId: String, websiteId: Int): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        jsonObject.put("userId", userId)
        jsonObject.put("websiteId", websiteId)
        val body = jsonObject.toString().toRequestBody()
        return HttpServiceService.service.deleteWebsiteCollect(body)
    }

    fun addWebsiteCollect(userId: String, nickName: String, url: String): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        jsonObject.put("userId", userId)
        jsonObject.put("nickName", nickName)
        jsonObject.put("url", url)
        val body = jsonObject.toString().toRequestBody()
        return HttpServiceService.service.addWebsiteCollect(body)
    }

    fun getPlatformList(): Flow<HttpResult<MutableList<PlatformItemBean>>> {
        return HttpServiceService.service.getPlatformList()
    }
}